package view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import dao.MenuDao;
import modle.Country;
import modle.Menu;
import util.DbUtil;
import util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

public class DelMenu extends JFrame {

	private JPanel contentPane;
	private JTextField Search;
	private JTextField Name;
	private JTextField Id;
	private JLabel ioi_Label;
	private JLabel noi_Label;
	MenuDao menuDao = new MenuDao();
	DbUtil dbUtil = new DbUtil();
	private JLabel sibi_Label;
	private JButton btnSearch;
	private JButton btnDel;
	private JButton btnBack;
	private JLabel label;
	private JLabel delLabel;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	private JLabel backgroundlabel;
	ResourceBundle rd;
	private JLabel Img;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DelMenu frame = new DelMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DelMenu() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(DelMenu.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 650, 551);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		delLabel = new JLabel("Delete Old Item in Menu");
		delLabel.setForeground(new Color(205, 133, 63));
		delLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		delLabel.setBounds(37, 13, 311, 46);
		contentPane.add(delLabel);
		
		Search = new JTextField();
		Search.setBounds(246, 151, 224, 33);
		contentPane.add(Search);
		Search.setColumns(10);
		
		Name = new JTextField();
		Name.setColumns(10);
		Name.setBounds(246, 228, 224, 33);
		contentPane.add(Name);
		
		Id = new JTextField();
		Id.setColumns(10);
		Id.setBounds(246, 300, 224, 33);
		contentPane.add(Id);
		
		ioi_Label = new JLabel("Id of Item: ");
		ioi_Label.setForeground(new Color(70, 130, 180));
		ioi_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		ioi_Label.setBounds(24, 297, 208, 33);
		contentPane.add(ioi_Label);
		
		noi_Label = new JLabel("Name of Item: ");
		noi_Label.setForeground(new Color(70, 130, 180));
		noi_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		noi_Label.setBounds(24, 225, 208, 33);
		contentPane.add(noi_Label);
		
		sibi_Label = new JLabel("Search Item by Id:");
		sibi_Label.setForeground(new Color(70, 130, 180));
		sibi_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		sibi_Label.setBounds(24, 151, 208, 33);
		contentPane.add(sibi_Label);
		
		btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		btnSearch.setBounds(504, 148, 114, 36);
		contentPane.add(btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("正在向数据库中插入菜单信息");
				searchActionPerformed(e);
			}
		});
		
		
		btnDel = new JButton("Delete");
		btnDel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 23));
		btnDel.setBounds(271, 401, 140, 38);
		contentPane.add(btnDel);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon(DelMenu.class.getResource("/img/admin2.png")));
		label.setBounds(408, 46, 77, 92);
		contentPane.add(label);
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("正在删除该菜单信息");
				delActionPerformed(e);
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(520, 13, 98, 32);
		contentPane.add(comboBox);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(24, 454, 98, 38);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Menu().setVisible(true);
				dispose();
			}
		});
		
		Img = new JLabel("");
		Img.setBounds(437, 346, 181, 146);
		contentPane.add(Img);
		
		fillCountries();
		
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(imgpath);
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}

	protected void searchActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String id = Search.getText();
		if (StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "ID不能为空");
			return;
		}
		
		System.out.println("正在查找该菜品的ID......");
		
		Connection con = null;
		try {
			con = dbUtil.getCon();
			Menu menu = menuDao.MenuSearch(con, id);
			if(menu!=null) {
				Name.setText(menu.getName());
				Id.setText(menu.getId());
				Img.setIcon(ResizeImage(menu.getPicture(), Img));
			}
			else {
				JOptionPane.showMessageDialog(null, "数据库中没有此ID，查找失败");
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				// 关闭数据库连接
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	
	protected void delActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String name = Name.getText();
		String id = Id.getText();
		
		if (util.StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "name不能为空");
			return;
		}
		if (util.StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "id不能为空");
			return;
		}
		
		Menu menu = new Menu(id);
		Connection con = null;
		
		try{
			con = DbUtil.getCon();
			int num = menuDao.MenuDel(con, menu);
			if(num>0) {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "菜品删除成功");
//				this.dispose();
			}else {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "不存在该菜品");
			}
		}catch(Exception e1){
			JOptionPane.showMessageDialog(null, "出现异常，菜品删除失败！");
			e1.printStackTrace();
		}finally {
			try {
				// 关闭数据库连接
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		ioi_Label.setText(rd.getString("ioi_Label"));
		noi_Label.setText(rd.getString("noi_Label"));
		btnDel.setText(rd.getString("btnDel"));
		btnSearch.setText(rd.getString("btnSearch"));
		delLabel.setText(rd.getString("delLabel"));
		sibi_Label.setText(rd.getString("sibi_Label"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
